package com.googlecode.vmock.utils;

/**
 * Utility methods for arrays.
 * 
 * @author marcin.jagus
 */
public class ArrayUtils {

    /**
     * Constructor for subclasses.
     */
    protected ArrayUtils() {
    }

    /**
     * Checks if the object is in the given array.
     * <p>
     * If the given array is <code>null</code> then <code>false</code> is returned.
     * 
     * @param array Given array
     * @param value Given object
     * @return <code>true</code> if the given object is in the given array
     */
    public static boolean contains(Object[] array, Object value) {
        if (array != null) {
            for (Object o : array) {
                if (ObjectUtils.equals(value, o)) {
                    return true;
                }
            }
        }

        return false;
    }

    /**
     * Returns a string representation of a given array.
     * <p>
     * Example representation: {John Doe,1,1.20}
     * <p>
     * If array is empty or <code>null</code> then '{}' is returned.
     * 
     * @param array Given array
     * @return String representation of a given array
     */
    public static String toString(Object[] array) {
        StringBuilder builder = new StringBuilder();

        builder.append("{");

        if (array != null && array.length > 0) {
            builder.append(array[0]);
            for (int i = 1; i < array.length; i++) {
                builder.append(",").append(array[i]);
            }
        }

        builder.append("}");

        return builder.toString();
    }
}
